package com.itheima.controller;

import com.itheima.client.UserClient;
import com.itheima.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.List;

@RestController
//@DefaultProperties(defaultFallback="allMethodFallback")
@DefaultProperties(defaultFallback = "allMethodFallback")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private UserClient userClient;

   @RequestMapping("/consumer1/{id}")
    public User findById(@PathVariable("id") Integer id){
        User user = restTemplate.getForObject("http://127.0.0.1:8081/user/"+id,User.class);
        return user;
    }
    @RequestMapping("/consumer2/{id}")
    public User findBy(@PathVariable("id") Integer id)
    {

        System.out.println(discoveryClient.getInstances("user-service"));
        URI uri = discoveryClient.getInstances("user-service").get(0).getUri();
        System.out.println(uri);
//        return restTemplate.getForObject(uri+"/user/"+id,User.class);
        return null;
    }
//    @HystrixCommand(fallbackMethod = "findByIdFallback")
    @RequestMapping("/consumer3/{id}")
    @HystrixCommand
    public String findByIds(@PathVariable("id") Integer id){
        String user = restTemplate.getForObject("http://user-service/user/"+id,String.class);
        return user;
    }


    @RequestMapping("/consumer4/{id}")
//    @HystrixCommand
        @HystrixCommand(fallbackMethod = "findByIdFallback2")
    public User findByIda(@PathVariable("id") Integer id){
        User user=userClient.findById(id);
       return user;
    }
   /* @GetMapping("/consumer/{id}")
    public User findById(@PathVariable("id") Integer id){
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        ServiceInstance serviceInstance = instances.get(0);
        URI uri = serviceInstance.getUri(); //http://DESKTOP-8V9MV76:8081
        User user = restTemplate.getForObject(uri+"/user/" + id, User.class);
        return user;
    }*/


/*    @GetMapping("/consumer/{id}")
    @HystrixCommand
    public String findById(@PathVariable("id") Integer id){
        String user = restTemplate.getForObject("http://user-service/user/" + id, String.class);
        return user;
    }*/



//    对findById方法降级 返回类型、参数数量和类型都要一致
    public String findByIdFallback( Integer id){
        return "服务器太忙。请稍后重试.............";
    }

    public User findByIdFallback2(Integer id) {
        User user=new User();
        user.setName("异常");
        return user;
    }

    //    对findById方法降级 返回类型、参数数量和类型都要一致
    public String allMethodFallback(){
        return "服务器太太太太太太忙。请稍后重试.............";
    }
}
